package wechat.handler.work;

import java.io.InputStream;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.handler.bean.WechatInformationBean;
import wechat.handler.resources.AppFilter;
import wechat.handler.resources.InstanceResource;
import wechat.handler.response.business.WechatInformationBusiness;
import wechat.handler.util.Configure;
import wechat.handler.util.StreamUtil;
import wechat.handler.util.aes.AesException;
import wechat.handler.util.aes.WXBizMsgCrypt;

public class WechatInformationHandler {
	private Logger log = LoggerFactory.getLogger(WechatInformationHandler.class);
	
	private static final String Appid = Configure.getAppid() ;
	private static final String EncodingAESKey = Configure.getEncodingAESKey();
	private static final String Token = Configure.getToken();
	
	private WXBizMsgCrypt wxBizMsgCrypt;
	
	public WechatInformationHandler() {
		try {
			wxBizMsgCrypt = new WXBizMsgCrypt(Token, EncodingAESKey, Appid);
		} catch (AesException e) {
			log.error("WechatInformationHandler -- WXBizMsgCrypt init fail",e);
			e.printStackTrace();
		}
	}
	
	
	public WechatInformationHandler(String appid) {
		try {
			InstanceResource instanceResource = AppFilter.chooseApp(appid);
			String token = instanceResource.getToken();
			String encodingAESKey = instanceResource.getEncodingAESKey();
			
			wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAESKey, appid);
		} catch (AesException e) {
			log.error("WechatInformationHandler -- WXBizMsgCrypt init fail",e);
			e.printStackTrace();
		}
	}
	
	public String process(WechatInformationBean wechatInformationBean) {
		return process(null, wechatInformationBean);
	}
	
	
	public String process(String appid,WechatInformationBean wechatInformationBean) {

		InputStream input = wechatInformationBean.getDataInputStream();

		String str = StreamUtil.inputStreamToString(input);
		System.out.println(str);
		log.info("WechatInformationHandler--Process appid=" + appid + " str=" + str);

		String respResult = "success";
		try {

			String msgSignature = wechatInformationBean.getMsgSignature();
			String timestamp = wechatInformationBean.getTimestamp();
			String nonce = wechatInformationBean.getNonce();

			String decryptMsg = wxBizMsgCrypt.decryptMsg(msgSignature, timestamp, nonce, str);
			log.info("WechatInformationHandler--Process decryptMsg= \n" + decryptMsg);

			WechatInformationBusiness wechatInformationBusiness = new WechatInformationBusiness();
			if (appid == null) {
				respResult = wechatInformationBusiness.execute(decryptMsg,wxBizMsgCrypt);
			}
			else {
				respResult = wechatInformationBusiness.execute(appid,decryptMsg,wxBizMsgCrypt);
			}
			

		} catch (DocumentException e) {
			log.error("WechatInformationHandler--process: fail",e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error("WechatInformationHandler--process: fail",e);
			e.printStackTrace();
		}

		return respResult;
	}
}
